<?php
class Search_Sphinx extends Model_Sphinx
{
	protected static $_instance = NULL;

	public static function instance()
	{
		if (NULL === self::$_instance)
		{
			$class = __CLASS__;
			self::$_instance = new $class;
		}

		return self::$_instance;
	}

	public function get_pc_result($key=NULL,$cond=array(),$sort=NULL,$limit=20,$offset=0)
	{
		if (!empty($key))
			$cond['key_word'] = $key;
		if (!empty($sort))
			$cond['sort']     = $sort;
			
		$sphinx_result = $this->get_pc_ids($cond,$limit,$offset);
		$pc_ids = Arr::get($sphinx_result,'ids');
		$count      = Arr::get($sphinx_result,'count');
		if (!empty($pc_ids))
		{
			$condition = array('id' => $pc_ids);
			$list = PingCe::search($condition);
		}
		else 
			$list = array();
		
		$result = array(
			'count' => $count,
			'list'  => $list,
		);
		
		return $result;
	}
	
	/**
	 * 获取促销信息搜索结果
	 */
	public function get_search_result($key=NULL,$cond = array(),$sort=NULL,$limit=20,$offset=0)
	{
		if (!empty($key))
			$cond['key_word'] = $key;
		if (!empty($sort))
			$cond['sort']     = $sort;
			
		$sphinx_result = $this->get_cuxiao_ids($cond,$limit,$offset);
		$cuxiao_ids = Arr::get($sphinx_result,'ids');
		$count      = Arr::get($sphinx_result,'count');
		if (!empty($cuxiao_ids))
		{
			$condition = array('id' => $cuxiao_ids,'status' => array('valid','breakrules'));
			$list = Cuxiao::search($condition);
		}
		else 
			$list = array();
		
		$result = array(
			'count' => $count,
			'list'  => $list,
		);
		
		return $result;
	}
	
	/**
	 * 搜索评测信息
	 * $condition = array(
	 *		'key_word' => 
	 * )
	 */
	public function get_pc_ids($condition = array(),$limit = 20,$offset=0)
	{
		$search_result = array(
			'ids'   => array(),
			'count' => 0,
		);
		
		$sphinx_condition = array();
		// 城市
		if (isset($condition['city']) && !empty($condition['city']))
			$sphinx_condition['city'] = $this->format_city($condition['city']);
		
		// 分类
		$sphinx_condition['category'] = Arr::get($condition,'category');
		// 品牌
		$sphinx_condition['brand'] = Arr::get($condition,'brand');
		// 关键词 
		$key_word = Arr::get($condition,'key_word');
		if (is_array($key_word))
			$sphinx_condition['key_word'] = join('|',$key_word);
		else
			$sphinx_condition['key_word'] = $key_word;
		// 排序
		$sphinx_condition['sort'] = Arr::get($condition,'sort');
		$result = $this->get_pc_search_result($sphinx_condition, $limit, $offset);
		
		$count = $result['total'];
		
		if ($count > 0)
		{
			$ids = array();
			if (isset($result['matches']))
			{
				foreach ($result['matches'] as $key => $item)
				{
					array_push($ids,$key);	
				}
				$search_result = array(
					'ids'	=> $ids,
					'count'	=> $count,
				);
			}
		}
		return $search_result;
	}

	/**
	 * 搜索促销信息
	 * $condition = array(
	 *		'key_word' => 
	 * )
	 */
	public function get_cuxiao_ids($condition = array(),$limit = 20,$offset=0)
	{
		$search_result = array(
			'ids'   => array(),
			'count' => 0,
		);
		
		$sphinx_condition = array();
// 		// 城市
// 		if (isset($condition['city']) && !empty($condition['city']))
// 			$sphinx_condition['city'] = $this->format_city($condition['city']);
		// 店铺
		if (isset($condition['shop']) && !empty($condition['shop']))
			$sphinx_condition['shop'] = $this->format_shop($condition['shop']);
		
		// 分类
		$sphinx_condition['category'] = Arr::get($condition,'category');
		// 品牌
		$sphinx_condition['brand'] = Arr::get($condition,'brand');
		// 区域
		$sphinx_condition['location'] = Arr::get($condition,'location');
		// 关键词 
		$sphinx_condition['key_word'] = Arr::get($condition,'key_word');
		// 排序
		$sphinx_condition['sort'] = Arr::get($condition,'sort');
		
		// 认证
		if (isset($condition['auth']) && !empty($condition['auth']))
			$sphinx_condition['auth'] = $this->format_auth($condition['auth']);
		$result = $this->get_cuxiao_search_result($sphinx_condition, $limit, $offset);
		
		$count = $result['total'];
		
		if ($count > 0)
		{
			$ids = array();
			if (isset($result['matches']))
			{
				foreach ($result['matches'] as $key => $item)
				{
					array_push($ids,$key);	
				}
				$search_result = array(
					'ids'	=> $ids,
					'count'	=> $count,
				);
			}
		}
		return $search_result;
	}
	
	private function get_cuxiao_search_result($condition, $limit, $offset)
	{
		$this->_nsphinx = new SphinxClient();
		$this->_nsphinx->SetServer(Kohana::config('sphinx.host'),Kohana::config('sphinx.port'));
		$this->_nsphinx->SetMatchMode( SPH_MATCH_EXTENDED2 );
		if (isset($condition['sort']) && !empty($condition['sort']))
		{
			$sort=intval($condition['sort']);
			$sort_value = Kohana::config('sphinx.sort_func.'.$sort.'.value');
			$this->_nsphinx->SetSortMode( SPH_SORT_EXTENDED , $sort_value );
		}
		else
			$this->_nsphinx->SetSortMode( SPH_SORT_EXTENDED , 'paid DESC, @relevance DESC, update_time DESC' );
		$this->_nsphinx->SetFieldWeights(array( 'title' => 90, 'descript' => 10));
		$query_string = '';
		if (isset($condition['key_word']) && !empty($condition['key_word']))
		{
			$keyword_arr = parent::segQueryString($condition['key_word']);
			$query_string = join(" ",$keyword_arr);
			$query_string = "@(title,descript) \'$query_string\' ";	
		}
		
		if (isset($condition['category']) && !empty($condition['category']))
		{
			$category = $condition['category'];
			$query_string .= " @classes \'$category\' ";
		}
		
		if (isset($condition['location']) && !empty($condition['location']))
		{
			$location = $condition['location'];
			$query_string .= " @location \'$location\' ";
		}
			
		if (isset($condition['city']) && !empty($condition['city']))
			$this->_nsphinx->SetFilter('city',$condition['city']);
		
		if (isset($condition['shop']) && !empty($condition['shop']))
			$this->_nsphinx->SetFilter('shop',$condition['shop']);
			
		if (isset($condition['brand']) && !empty($condition['brand']))
			$this->_nsphinx->SetFilter('brand',$condition['brand']);
		
		if (isset($condition['auth']) && !empty($condition['auth']))
		{
			$this->_nsphinx->SetFilter('auth',$condition['auth']);
		}
			
		if (isset($condition['shop_status']) && !empty($condition['shop_status']))
		{
			$status_types = Kohana::config('shopinfo.open_status');
			$shop_status  = Arr::get($status_types, $condition['shop_status'], -1);
			
			if ($shop_status >= 0)
				$this->_nsphinx->SetFilter('shop_status',array($shop_status));
		}
		//else       ///默认返回开通的商铺的促销信息
		//	$this->_nsphinx->SetFilter('shop_status', array('1'));
			
		$this->_nsphinx->SetLimits(intval($offset),intval($limit), 5000);
		$result = $this->_nsphinx->Query($query_string,Kohana::config('sphinx.cuxiao_index_name'));
		
		return $result;
	}
	
	private function get_pc_search_result($condition, $limit, $offset)
	{
		$this->_nsphinx = new SphinxClient();
		$this->_nsphinx->SetServer(Kohana::config('sphinx.host'),Kohana::config('sphinx.port'));
		$this->_nsphinx->SetMatchMode( SPH_MATCH_EXTENDED2 );
		if (isset($condition['sort']) && !empty($condition['sort']))
		{
			$sort=intval($condition['sort']);
			$sort_value = Kohana::config('sphinx.sort_func.'.$sort.'.value');
			$this->_nsphinx->SetSortMode( SPH_SORT_EXTENDED , $sort_value );
		}
		else
			$this->_nsphinx->SetSortMode( SPH_SORT_EXTENDED , '@relevance DESC, pv DESC' );
		$this->_nsphinx->SetFieldWeights(array( 'title' => 90, 'brand_name'=>30, 'descript' => 10));
		$query_string = '';
		if (isset($condition['key_word']) && !empty($condition['key_word']))
		{
			$keyword_arr = parent::segQueryString($condition['key_word']);
			$query_string = join(" ",$keyword_arr);
			$query_string = "@(title,brand_name,descript) \'$query_string\' ";	
		}
		
		if (isset($condition['category']) && !empty($condition['category']))
		{
			$category = $condition['category'];
			$query_string .= " @classes \'$category\' ";
		}
			
		if (isset($condition['city']) && !empty($condition['city']))
			$this->_nsphinx->SetFilter('city',$condition['city']);

			
		if (isset($condition['brand']) && !empty($condition['brand']))
			$this->_nsphinx->SetFilter('brand',$condition['brand']);
		
		$this->_nsphinx->SetLimits(intval($offset),intval($limit), 5000);
		$result = $this->_nsphinx->Query($query_string,Kohana::config('sphinx.pc_index_name'));
		
		return $result;
	}
	
	/*
	 * 获取排序用的int数值
	 */
	public static function get_sort_value ($order, $by)
	{
		$default_value = Arr::get(Kohana::config('sphinx.sort_value'),'default',99);
		
		$sort_value_array = Arr::get(Kohana::config('sphinx.sort_value'),$order);
		
		if (empty($sort_value_array) || !is_array($sort_value_array))
			return $default_value;
			
		$sort_value = Arr::get($sort_value_array,$by,NULL);
		if (empty($sort_value))
			return $default_value;
			
		return $sort_value;
	}
	
	private function format_shop($shop)
	{
		if (!is_array($shop))
			$shop = array($shop);
		
		return $shop;
	}
	
	private function format_city($city)
	{
		if (!is_array($city))
			$city = array($city);
		
		return $city;
	}
	
	private function format_auth($auth)
	{
		$auth = array(1,2,3,4);
		return $auth;
	}
	
}
